package com.lzy.dao.key;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.lzy.dao.HibernateDao;
import com.lzy.entity.KeyIM;
import com.lzy.util.Page;

@Component
public class KeyIMDaoImpl extends HibernateDao implements KeyIMDao{
	
	public boolean isExist(String id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(id) from KeyIM where id=?");
		query.setParameter(0, id);
		long count = (Long) query.uniqueResult();
		return count > 0 ? true:false;
	}

	public KeyIM queryById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from KeyIM where id=?");
		query.setParameter(0, id);
		return (KeyIM) query.uniqueResult();
	}

	public Page<KeyIM> queryByCatalogId(int id) {
		Page page = new Page();
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from KeyIM where catalogId=?");
		query.setParameter(0, id);
		List list = query.list();
		page.setResult(list);
		return page;
	}

	public void add(KeyIM keyIM) {
		Session session = sessionFactory.getCurrentSession();
		session.save(keyIM);
	}

	public void del(String id) {
		Session session = sessionFactory.getCurrentSession();
		Object object = session.load(KeyIM.class, id);
		session.delete(object);
	}

	public void update(KeyIM keyIM) {
		Session session = sessionFactory.getCurrentSession();
		session.update(keyIM);
	}
	
	
}
